<template>
  <view>
    <my-goods v-for="goods in goodsList" :key="goods.goods_id" :goods="goods" />
  </view>
</template>

<script>
import { getGoodsListApi } from '@/api/index'
import myGoods from '../../conponents/my-goods/my-goods.vue';
export default {
  components: { myGoods },
  data () {
    return {
      queryData: {
        query: '',
        cid: '',
        pagenum: 1, //页码
        pagesize: 10 //每页数量
      },
      isTip: false,
      goodsList: [],
      total: 0,
      isLoadingMore: false //是否正在上拉刷新
    };
  },

  onLoad (options) {
    console.log(options)
    this.queryData.query = options.query || ''
    this.queryData.cid = options.cid || ''

    this.getGoodsList()
  },

  onPullDownRefresh () {
    this.queryData.pagenum = 1
    this.goodsList = []
    this.getGoodsList(this.isTip)
  },

  onReachBottom () {
    if (this.isLoadingMore) return
    if (this.goodsList.length >= this.total) {
      uni.showToast({
        title: '没有更多数据啦~',
        duration: 800,
      })

      return
    }
    this.queryData.pagenum++
    this.getGoodsList()
  },

  methods: {
    async getGoodsList (isTip) {
      this.isLoadingMore = true
      const { meta: { status }, message: { goods, total } } = await getGoodsListApi(this.queryData, isTip)

      if (status === 200) {
        this.goodsList = [...this.goodsList, ...goods]
        this.total = total
        this.isLoadingMore = false

        // 如果是下拉刷新，数据回来了，则停止默认下拉刷新
        if (!this.isTip) {
          wx.stopPullDownRefresh()
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>